package ev.space;

import ev.utility.*; 
import ev.traffic.*;

import swarm.Globals;
import swarm.defobj.Zone;
import swarm.collections.List;
import swarm.collections.ListImpl;
import swarm.collections.Index;
import swarm.space.Grid2dImpl;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;

public class CitySpace extends Grid2dImpl{
		private int [] nodeList = null;
		public static final ArrayList<Side> roadMap = new ArrayList<Side>();   // the edge info
		public static final ArrayList<Nodes> nodeMap = new ArrayList<Nodes>();   // the node info

		public CitySpace(Zone aZone,int x,int y){
				super(aZone,x,y);
				fastFillWithObject(null);
				this.createMap();
		}
		public static Nodes getNodes(int nodeNum){
				for(Nodes nodes:nodeMap){
						if(nodes.getNum() == nodeNum)
								return nodes; 
				}
				return null;
		}
		public static Nodes getNodes(int xPos,int yPos){
				for(Nodes nodes:nodeMap){
						if(xPos == nodes.getXpos()&& yPos == nodes.getYpos()){
								return nodes;
						}
				}
				return null;
		}
		private void createMap(){
				//roadMap.txt format:nodeNum xPos yPos nodeNum xPos yPos weight
				DataReader dr = new DataReader("bin/ev/source/roadMap.txt");
				String temp = "";
				String[] a = new String[10];
				ArrayList<Integer> tempNodeList = new ArrayList<Integer>();
				Nodes nodes = null;

				while((temp = dr.readLine()) != null){
						a = temp.split(" ");
						Side side = new Side(Integer.parseInt(a[0]),Integer.parseInt(a[3]),Integer.parseInt(a[6]));
						int node = Integer.parseInt(a[0]);
						nodes = new Nodes(node,Integer.parseInt(a[1]),Integer.parseInt(a[2]));
						if(!tempNodeList.contains(node)){
								putObject$atX$Y(nodes,Integer.parseInt(a[1]),Integer.parseInt(a[2]));
								tempNodeList.add(node);
								nodeMap.add(nodes);
						}
						roadMap.add(side);
				}
				int[] tempList = new int[tempNodeList.size()];
				for(int i = 0;i < tempNodeList.size();i++){
						tempList[i] = tempNodeList.get(i);
				}
				nodeList = tempList;

				dr.close();
		}
		public static int getWeight(int preNode,int node){
				Side side = getSide(preNode,node);
				if(side != null){
						return side.getWeight();
				}
				return -1;
		}
		public static Side getSide(int preNode,int node){
				for(Side side:roadMap){
						if((side.getPreNode() == preNode)&&(side.getNode() == node))
								return side;
				}
				return null;
		}
		public int[] getNodeList(){
				return this.nodeList;
		}
}
